package com.baidu.times;
import com.baidu.entity.Borr;
import com.baidu.entity.Sked;
import com.baidu.entity.TbyRegister;
import com.baidu.entity.WycOverdue;
import com.baidu.fegin.MessageFegin;
import com.baidu.mapper.BorrMapper;
import com.baidu.mapper.SkedMapper;
import com.baidu.mapper.TbyRegisterMapper;
import com.baidu.mapper.WycOverdueMapper;
import com.baidu.utils.ResultResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Description: 定时任务 每天上午8点执行
 * @Author: 王雨超
 * @Date: 2021/12/13 8:38
 */
@Component
@Slf4j
@SuppressWarnings({"all"})
public class TimedTask {
    @Autowired
    private SkedMapper skedMapper;
    @Autowired
    private BorrMapper borrMapper;
    @Autowired
    private MessageFegin messageFegin;
    @Autowired
    private TbyRegisterMapper tbyRegisterMapper;
    @Autowired
    private WycOverdueMapper wycOverdueMapper;
    //cron="0 0 8 * * ？"  秒 分 时 日 月 周 年
//    @Scheduled(cron="0 0 8 * * ？")
    public void times(){
        TimedTask timedTask = new TimedTask();
        //获取当前日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //第一步查询还款表
        List<Sked> skeds = skedMapper.selectList(new QueryWrapper<Sked>());
        for (Sked sked : skeds) {
            //数据库中的还款时间
            long s = Long.parseLong(sdf.format(sked.getTimes()));
            //当前日期
            long d = Long.parseLong(sdf.format(new Date()));
            //还款时间和当前日期进行比较
            // 如果还款日期>当前日期 7天之内的情况下 短信进行提醒
            //如果还款日期=当前日期 证明当天是还款日期 进行短信提醒 进行自动扣款
            if(s==d){
                Borr borr = borrMapper.selectById(sked.getBid());
                TbyRegister tbyRegister = tbyRegisterMapper.selectById(borr.getUserid());
                ResultResponse resultResponse1 = timedTask.sendMessage("2", "今天是您的还款日" + sdf1.format(new Date()) + ",请保持余额充足！", borr.getUserid() + "", tbyRegister.getIphone());
                log.info("{}",resultResponse1);
                //进行扣款
                //清除借款记录
                //往还款记录中记录
            }
            //如果还款日期<当前日期 证明该客户逾期
            if(s<d){
                //判断是未还的情况下
                if(sked.getWon()==0){
                    //查询接口记录
                    Borr borr = borrMapper.selectById(sked.getBid());
                    //先去数据库查询该记录是否在逾期表中
                    WycOverdue wycOverdue1 = wycOverdueMapper.selectOne(new QueryWrapper<WycOverdue>().eq("bid", borr.getBid()));
                    //计算出逾期的天数
                    Long tian=d-s;
                    //如果数据库不存在该用户的逾期信息 则添加到逾期表中
                    if(wycOverdue1==null){
                        //放入数据库
                        WycOverdue wycOverdue = new WycOverdue();
                        wycOverdue.setUid(borr.getUserid());//用户id
                        wycOverdue.setNumber(Integer.parseInt(tian+""));//逾期天数
                        wycOverdue.setCollection(1);//催收方式 1:短信 默认为1
                        wycOverdue.setPrincipal(sked.getPrincipal());//本金
                        wycOverdue.setInterest(sked.getInterest());//利息
                        wycOverdue.setStatus(2);//未还款
                        wycOverdue.setInformtime(new Date());//当前时间
                        wycOverdue.setBid(sked.getBid());//借款记录id
                        wycOverdue.setCount(1);//通知次数
                        wycOverdue.setFeecount(0);//扣费次数
                        wycOverdue.setPid(borr.getPid());//产品id
                        wycOverdueMapper.insert(wycOverdue);//添加到逾期表中
                        log.info("{}",wycOverdue);
                        //短信提醒
                        TbyRegister tbyRegister = tbyRegisterMapper.selectById(borr.getUserid());
                        ResultResponse resultResponse = timedTask.sendMessage("2", tbyRegister.getSname() + "您好，你的贷款已逾期，逾期天数为：" + tian + "，请及时还款!", borr.getUserid() + "", tbyRegister.getIphone());
                        log.info("{}",resultResponse);
                    }else{
                        //不为空的情况下 证明该记录已经存在表中 通知次数+1
                        wycOverdue1.setCount(wycOverdue1.getCount()+1);
                        wycOverdueMapper.updateById(wycOverdue1);
                        log.info("{}",wycOverdue1);
                    }
                    //对逾期的天数进行判断
                    if(tian<=7){
                        //短信提醒
                        TbyRegister tbyRegister = tbyRegisterMapper.selectById(borr.getUserid());
                        ResultResponse resultResponse = timedTask.sendMessage("2", tbyRegister.getSname() + "您好，你的贷款已逾期，逾期天数为：" + tian + "，请及时还款!", borr.getUserid() + "", tbyRegister.getIphone());
                        log.info("{}",resultResponse);

                    }
                    //如果逾期天数在7~90天中
                    if(tian<=90&&tian>7){
                        //短信提醒+信誉积分扣除
                    }
                }
            }
        }
    }
    //发送短信
    public ResultResponse sendMessage(String code,String message,String uid,String phone){
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",code);
        map.put("message",message);
        map.put("uid",uid);
        map.put("phone",phone);
        return messageFegin.sendMessage(map);
    }
}
